from fastapi import APIRouter
from app.core.shared import cpu_monitor, vm_monitor, network_monitor, disk_monitor

chart_route = APIRouter(prefix="/api/chart", tags=["系统摘要"])

@chart_route.get("/cpu/history", response_model=dict)
def get_cpu_history():
    cpu_history = cpu_monitor.cpu_history
    time_history = cpu_monitor.time_history
    return {
        "title": "CPU 负载趋势",
        "x": time_history[-20:],
        "y": cpu_history[-20:],
        "unit": "%",
    }

@chart_route.get("/memory/history", response_model=dict)
def get_memory_history():
    memory_history = vm_monitor.memory_history
    time_history = vm_monitor.time_history
    return {
        "title": "内存使用率趋势",
        "x": time_history[-20:],
        "y": memory_history[-20:],
        "unit": "%",
    }

@chart_route.get("/disk/history", response_model=dict)
def get_disk_history():
    time_history = disk_monitor.time_history
    return {
        "title": "磁盘使用率趋势",
        "read": disk_monitor.disk_history["读入速度"][-20:],
        "write": disk_monitor.disk_history["写入速度"][-20:],
        "x": time_history[-20:],
        "unit": "MB/s",
    }

@chart_route.get("/network/history", response_model=dict)
def get_network_history():
    network_history = network_monitor.network_history
    time_history = network_monitor.time_history
    return {
        "title": "网络流量趋势",
        "x": time_history[-20:],
        "y": network_history[-20:],
        "unit": "kbps",
    }




